package sein.android.hopenote;

import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class User_List_Adapter  extends BaseAdapter{
	
	static Context context = null;
	int layoutId = 0;
	static ArrayList<User_Data> ResultDataArr = null;
	LayoutInflater Inflater = null;
	private DBAdapter HnopenHelper;
	private static SQLiteDatabase sdb;
	private static String str_user;
	private static Cursor cursor;
	
	User_List_Adapter(Context _context, int _layoutId, ArrayList<User_Data> _ResultDataArr){
		context = _context;
		layoutId = _layoutId;
		ResultDataArr = _ResultDataArr;
		Inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		HnopenHelper = new DBAdapter(context);
        sdb = HnopenHelper.getReadableDatabase();
	}
	
	public int getCount(){
		return (ResultDataArr == null) ? 0 : ResultDataArr.size();
	}
	
	public Object getItem(int position){
		return (getCount() > position) ? ResultDataArr.get(position) : null;
	}
	
	public long getItemId(int position)
	{
		return position;
	}
	
	@SuppressWarnings("unused")
	public View getView(final int position, View convertView, ViewGroup parent){
		final int pos = position;
		if(convertView == null){
			convertView = Inflater.inflate(layoutId, parent,false);
		}
		TextView tv_name = (TextView)convertView.findViewById(R.id.row_name);
		tv_name.setText(ResultDataArr.get(position).user_name);
		TextView tv_age = (TextView)convertView.findViewById(R.id.row_age);
		tv_age.setText(ResultDataArr.get(position).user_age);
		TextView tv_sex = (TextView)convertView.findViewById(R.id.row_sex);
		if(ResultDataArr.get(position).user_sex.equals("0"))
		{
			tv_sex.setText("남자");
		}
		else
		{
			tv_sex.setText("여자");
		}
		//tv_sex.setText(ResultDataArr.get(position).user_sex);
		
		Button row_del = (Button)convertView.findViewById(R.id.row_del);
		Button row_update = (Button)convertView.findViewById(R.id.row_update);
		
		row_del.setOnClickListener(new View.OnClickListener() {			
        	public void onClick(View v) {
				// TODO Auto-generated method stub
				//str_user = HopeNoteDb.CURRENT_USER;
        		str_user = ResultDataArr.get(pos).user_name;
				String qWhere = HopeNoteDb.COL_USER + " = '" + str_user + "'";
				sdb.delete(HopeNoteDb.TB_USER, qWhere, null);
				Toast.makeText(context, "삭제 완료", Toast.LENGTH_SHORT).show();
			
				
				//reset user list
				cursor = sdb.query(HopeNoteDb.TB_USER, new String[] {HopeNoteDb.COL_USER,HopeNoteDb.COL_SEX,HopeNoteDb.COL_AGE}, null, null, null, null, null);
		    	cursor.moveToFirst();
		        ResultDataArr.clear();
		        User_Data datarow;
		        for(int i=0; i < cursor.getCount(); i++)
		        {
		        	datarow = new User_Data(cursor.getString(0),cursor.getString(1),cursor.getString(2).substring(0,10));
		        	ResultDataArr.add(datarow);
		        	cursor.moveToNext();
		        }
		        cursor.close();
		        HopeNoteDb.CURRENT_USER = "";
		        Intent intent_main = new Intent("BR_Main");
				context.sendBroadcast(intent_main);
				
				SharedPreferences cache = context.getSharedPreferences("cache", Activity.MODE_PRIVATE);
				SharedPreferences.Editor editor = cache.edit();
				editor.putString("USER", null);
				editor.commit();
				notifyDataSetChanged();
			}
		});
		row_update.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				HopeNoteDb.MODIFY_USER = ResultDataArr.get(pos).user_name;
				Intent intent = new Intent(context, User_Update.class);
				context.startActivity(intent);
				
			}
		});

		return convertView;
	}

	public static void Reset() {
		// TODO Auto-generated method stub
		cursor = sdb.query(HopeNoteDb.TB_USER, new String[] {HopeNoteDb.COL_USER,HopeNoteDb.COL_SEX,HopeNoteDb.COL_AGE}, null, null, null, null, null);
    	cursor.moveToFirst();
        ResultDataArr.clear();
        User_Data datarow;
        for(int i=0; i < cursor.getCount(); i++)
        {
        	datarow = new User_Data(cursor.getString(0),cursor.getString(1),cursor.getString(2).substring(0,10));
        	ResultDataArr.add(datarow);
        	cursor.moveToNext();
        }
        cursor.close();
        //Intent intent_main = new Intent("UserListReset");
		//context.sendBroadcast(intent_main);
		
		
	}
}
